import { Shape, ShapeKey, ShapeType } from "@/mdesign-lib/mdesign-graph.umd.js";
import { modelService, shapeService } from "@/mdesign-lib/mdesign-app.umd.js";

const defaultConfig = ["hidePorts", "showPartsAndPorts", "showRelationEdge", "showRelatedElement", "showInternalPort"];

const config = {
  [ShapeKey.Package]: ["showRelationEdge", "showInnerItem", "showRelatedElement"],
  [ShapeKey.Model]: ["showRelationEdge", "showInnerItem", "showRelatedElement"],
  [ShapeKey.Profile]: ["showRelationEdge", "showInnerItem", "showRelatedElement"],
  [ShapeKey.DiagramIcon]: ["showRelationEdge", "showRelatedElement"],
  [ShapeKey.UseCase]: ["showRelationEdge", "showRelatedElement"],
  [ShapeKey.Stereotype]: ["showRelationEdge", "showRelatedElement"],
  [ShapeKey.Class]: ["showRelationEdge", "showRelatedElement"],
  [ShapeKey.ActivityDiagram]: ["showActivityParameterNode"]
};

const label: Record<string, string> = {
  hidePorts: "隐藏所有端口",
  showPartsAndPorts: "显示端口/部件",
  showRelationEdge: "显示关系连线",
  showInnerItem: "显示内部元素",
  showRelatedElement: "显示关联元素",
  showTypeAndMultiplicity: "显示类型和多重性",
  showInternalPort: "显示所有端口",
  showActivityParameterNode: "活动参数节点",
  batchShowRelationEdge: "显示元素间连线"
};

/**
 * @param shape
 * @param elementType
 * @param diagramKey 画布类型
 * @param {Shape[]} shapes 当前框选的多个图元对象，没有框选时，为空数组或不传
 * @returns
 */
export const getDisplayRelatedItem = async (
  shape: Shape,
  elementType: ShapeKey,
  diagramKey: ShapeKey,
  shapeMap: Map<string, Shape>,
  shapes?: Shape[]
) => {
  let displayMenu: string[];
  const isMore = shapes && shapes.length > 1 ? true : false; // 框选多个图元
  const menuDisabledMap: Record<string, boolean> = {};
  if (elementType === ShapeKey.ActivityDiagram) {
    displayMenu = config[elementType] || defaultConfig;
  } else {
    const filterShapeTypes = new Set([ShapeType.Symbol, ShapeType.Port, ShapeType.CompartmentProperty]);
    const modelIds =
      shapes
        ?.filter(s => filterShapeTypes.has(s.shapeType) && s.modelId)
        .map(it => it.modelId)
        .distinct() || [];
    displayMenu = await modelService.getDisplayMenu(app.activeProject!.id, isMore ? modelIds : shape.modelId);
    if (shape.modelId) {
      // todo 提供是否存在的接口，而不是把所有部件和端口查询出来
      // const { toCreatePortModels, toCreatePartModels, existPortShapes } = await shapeService.queryPortAndPart(shape.id, shape.diagramId, app.activeProject!.id);

      // 如果要创建端口长度为0，说明端口已经全部显示在画布上了
      // if (!toCreatePortModels?.length) {
      //   displayMenu = displayMenu.filter(i => i !== 'showInternalPort');
      // }
      // if (toCreatePortModels.length === 0 && toCreatePartModels.length === 0) {
      //   displayMenu = displayMenu.filter(i => (i !== 'showInternalPort'));
      // }
      // if (!existPortShapes?.length) {
      //   displayMenu = displayMenu.filter(i => i !== 'hidePorts');
      // }
      // 隔间里的属性不显示“显示关联元素”菜单
      // const parent = shape.parentId && shapeMap.get(shape.parentId);
      // if (parent && parent.shapeType === ShapeType.Compartment) {
      //   displayMenu = displayMenu.filter((i) => i !== 'showRelatedElement');
      // }
      if (displayMenu.includes("showInnerItem")) {
        if (!isMore) {
          const data = shape.modelId ? await shapeService.getInnerElement(shape) : [];
          const modelIds2 = data.filter(row => row.metaclass !== "Image").map(row => row.id);
          if (shape.style.imageBox || modelIds2.length === 0) {
            // 两种情况disable ：当前状态为图片模式，2有除了image之外的子元素，子元素但被移出数量为0
            menuDisabledMap.showInnerItem = true;
          }
        } else {
          // TODO额外新增disable : 批量选择图元时，暂时不支持显示内部元素
          menuDisabledMap.showInnerItem = true;
        }
      }
      // 序列图、活动图、状态机图组件区的元素没有禁用显示关系连线和显示关联元素
      if ([ShapeKey.InteractionDiagram, ShapeKey.ActivityDiagram, ShapeKey.StateMachineDiagram].indexOf(diagramKey) > -1) {
        displayMenu = displayMenu.filter(it => it !== "showRelationEdge" && it !== "batchShowRelationEdge" && it !== "showRelatedElement");
      }
      // 不存在批量显示关系连线时，框选多个图元需要删除【显示关系连线】菜单
      if (shapes && shapes.length > 1 && !displayMenu.includes("batchShowRelationEdge")) {
        displayMenu = displayMenu.filter(i => i !== "showRelationEdge");
      }
      // 框选元素中存在部件属性隔间图元时，不展示【显示关联元素】【显示端口/部件菜单】
      if (isMore ? shapes!.some(s => s.shapeType === ShapeType.CompartmentProperty) : shape.shapeType === ShapeType.CompartmentProperty) {
        displayMenu = displayMenu.filter(
          i => i !== "showRelatedElement" && i !== "showInternalPort" && i !== "hidePorts" && i !== "showPartsAndPorts"
        );
      }
    }
  }

  if (displayMenu.length === 0) {
    return [
      {
        label: "无",
        value: ""
      }
    ];
  } else {
    return displayMenu.map(key => {
      return {
        label: label[key],
        value: key,
        disabled: !!menuDisabledMap[key],
        supportBatchShowRelationEdges: displayMenu.includes("batchShowRelationEdge") // 支持批量显示关系连线
      };
    });
  }
};
